<?php
/**
 * 胜创短信
 * Create by PhpStorm
 * User: lijia
 * Data: 2020/8/14
 * Time: 14:48
 */

namespace app\common\controller;


use think\Exception;

class Sms
{

    public	$user 		= 'xc10207';	//短信账户		默认liyi
    public	$password 	= 'tx0207';	//账户密码
    public	$type 		= '';	//发送短信分类	默认华信
    public	$time 		= 0;		//发送短信间隔 	默认120秒
    public	$length 	= 0;		//验证码位数	默认6位
    public	$signature 	= '同兴农业';	//签名		默认千翼
    private	$ip;					//当前用户ip

    /**
     * 构造函数
     * @param string $type			发送短信分类
     * @param string $user			短信账户
     * @param string $password		账户密码
     * @param string $signature		签名
     * @param string $time			发送短信间隔 默认120秒
     * @param string $length		验证码位数	默认4位
     */
    function __construct($type=null,$user=null,$password=null,$signature=null,$time=null,$length=null){
        //修改默认值
        $this ->type		= $type?$type:$this ->type;
        $this ->user		= $user?$user:$this ->user;
        $this ->password 	= $password?$password:$this ->password;
        $this ->signature 	= $signature?$signature:$this ->signature;
        $this ->time 		= $time?$time:$this ->time;
        $this ->length 		= $length?$length:$this ->length;
        $this ->ip 			= $_SERVER["REMOTE_ADDR"];
    }

    /**
     * 发送验证码
     * @param $phone
     * @param $text
     * @return array
     * @throws Exception
     */
    public function sendMsg($phone,$text) {
        set_time_limit(0);
        $content="【".$this->signature."】".$text;
        $phone_regex = "/^1[3456789]\d{9}$/";
        if(!preg_match($phone_regex,$phone)){
            throw new Exception('输入手机号格式不正确',1000);
        }
        $smsapi = "http://47.92.214.24/smsJson.aspx";
        $pass = md5($this ->password);
        $sendurl = $smsapi.'?action=send&userid=&account='.$this->user.'&password='.$pass.'&mobile='.$phone.'&content='.$content.'&sendTime=&extno=';
        $sendurl=urldecode($sendurl);
        $result =file_get_contents($sendurl);
        $result = json_decode($result);
        $data = array(
            'code'=>$result->successCounts,	//短信回馈接口码 0  失败  1成功
            'msg'=>$result->message			//短信回馈文字描述
        );
        return $data;
    }

}